import { type } from "os";
import { modules } from "./modules";

/**
 *  1. 取到每一个namespace中的核心的getters
 *  2. user/isLogin
 *  3. user/isLogin的returnType取出来
 * */ 

// 跟着视频来的
// type GetGetter<Module> = Module extends { getters: infer G }?G: unknown;

// type GetGetters<Modules> = {
//     [k in keyof Modules]: GetGetter<Modules[k]>
// }

// type WZCGetters = GetGetters<typeof modules>;
// type addPrefix<P, K> = `${P & string}/${K & string}`

// type GetSpliceKey<P, Module> = addPrefix<P,keyof Module>

// type GetSpliceKeys<Modules> = {
//     [K in keyof Modules]: GetSpliceKey<K, Modules[K]>
// }[keyof Modules]

// type xx = GetSpliceKeys<WZCGetters>

// type GetFunc<T, A, B> = T[A & keyof T][B & keyof T[A & keyof T]]

// type GetSpliceObj<T> = {
//     [ K in GetSpliceKeys<T> ]: K extends `${ infer A}/${ infer B }` 
//     ? GetFunc<T, A, B> : unknown
// };

// type ModuleGetters = GetSpliceObj<WZCGetters>
// type Getters = {
//     [K in keyof ModuleGetters]: ReturnType<ModuleGetters[K]>
// }


// 自己写的
type GetterInfo<T> = {
    [k in keyof T]: T[k] extends { 'getters': infer X } ? X: never
}
// 去除user和wechat中getters以外的属性
type GetGetters = GetterInfo<typeof modules>

// 获得 'user/getters'|'wechat/getters'
type addPrefix<P, K> = `${P & string}/${K & string}`
type GetSpliceKeys<T extends keyof K, K> = T extends string ? addPrefix<T, keyof K[T]>: never

type KeysGetter = GetSpliceKeys<keyof GetGetters, GetGetters>  // "user/isLogin" | "wechat/isLogin"
// 获得GetGetters中user和wechat的getters返回值类型
type getReturnType<T, A, B> = T[A & keyof T][B & keyof T[A & keyof T]] extends (...any)=> infer X ? X : never
// 得到最终结果
type Getters = {
    [k in KeysGetter] : k extends `${infer X}/${infer Y}` ? getReturnType<GetGetters, X, Y> : never
}


export type { 
    Getters
}